# dgit document translation machinery
#
# Translators: there is no need to look at this file.  It contains
# complicated machinery which you do not need to understand :-).
# See po/README instead.

# Copyright (C)2018 Ian Jackson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# ----- devel docs (translators, please ignore all this) -----
#
# We use po4a for everything.
#
# po4a is nicely automatic for all the po handling, and the config
# file makes nice documentation, but it really really likes to dirty
# the tree.  And we want to automatically list the documents and the
# translation languages which it doesn't make easy.
#
# The approach I have chosen is as follows:
#
# po4a.cfg
#
#   This is autogenerated.  But also, we invite translators to add
#   their own language to it.  The resulting merge
#   conflict/discrepancy will be just an alphabetical ordering
#   discrepancy, since our generation script scans the available po
#   files.
#
#   Generation is done by a shell script, ./list-documents.
#
#   make targets:  po4a.cfg           # many others imply this
#                  po4a.cfg.check     # fails if out of date
#
# *.pot, *.po
#
#   As far as translators are concerned, generated and maintained by
#   po4a and committed to git.  As for the developers, we update this
#   occasionally just by running po4a.
#
#   make targets:  default           } mean
#                  pofiles           }  the same
#
# Translations for translators:
#
#   make targets:  preview.LANG
#                  translated
#
# Translated document source-language files (eg, translated pod):
#
#   Like with normal po things, the tools don't like to generate
#   output without updating (dirtying) the .po files.  But with po4a
#   this is really hard to avoid.  So we use a linkfarming technique.
#
#   make targets:  translated-only
#
# Translated output files (roff format, from pod2man):
#
#   We have sort of lost the usual make rule information by now
#   about targets and what to build.  Only po4a knows which
#   translated files are supposed to exist.  So we have a shell
#   script which reuses the top-level Makefile to run pod2man
#   where appropriate, and generate a further even-more-output-ish
#   tree.
#
#   make targets:  install
#
# Check:
#
#   make targets:  check
#                  pairwise-pocheck.check


default: pofiles

include ../i18n-common.make
PO4A ?= po4a

all update: translated

check: po4a.cfg.check pairwise-pocheck.check

pofiles: po4a.cfg
	$(PO4A) --no-translations $<

translated: po4a.cfg
	$(PO4A) $<

po4a.cfg: list-documents
	./$< $o

po4a.cfg.check: list-documents
	./$< >$@.tmp
	diff po4a.cfg $@.tmp

pairwise-pocheck.check:
	./pairwise-pocheck

t = ../.po4a.translated-only
p = ../po4a

translated-only: po4a.cfg pairwise-pocheck.check
	rm -rf $t && mkdir $t
	cd $t && ln -s $p/*.po $p/*.pot $p/po4a.cfg .
	cd $t && $(PO4A) po4a.cfg
	! test -d $t/translated || \
	cp -al --remove-destination $t/translated .

.po4a.%.cfg: po4a.cfg Makefile
	$S perl -p -e 's{^(\[po4a_langs\]).*}{$$1 $*};' \
		-e 's{:translated/man}{:.$*.translated/man}' \
		$< $o

.PRECIOUS: .po4a.%.cfg

pairwise-pocheck.%.check:
	@tclsh /dev/null || printf ':;:; Please run   apt-get install tcl\n'
	./pairwise-pocheck $*

preview.%: .po4a.%.cfg pairwise-pocheck.%.check
	$S $(PO4A) $<
	$S ./install-documents >/dev/null .$*.translated $@
	@ echo '# runes to preview the various final translated manpages:'
	$S find $@ -type f -printf 'man -l %p\n'

# We can't really use timestamps to drive po4a
.PHONY: preview.% translated

install: translated-only
	./install-documents translated $(DESTDIR)
